<?php
namespace app\admin\controller;

use app\admin\model\ChannelReport;
use app\services\XLSXWriter;
use think\Db;

/**
 * 报表管理
 * @Author    lf
 * @DateTime  2018-07-10
 */
class OrderReport extends Father {
	/**
	 * 日报表汇总
	 * @Author   lf
	 * @DateTime 2019-01-04T14:44:03+0800
	 * @param    string                   $seletime [description]
	 * @return   [type]                             [description]
	 */
	public function index() {

		$request = request()->only([
			'date' => date("Y-m-d"),
		]);

		$request['channel_id'] = session('relate_id');

		if (input('get.api')) {
			$orderReport = new ChannelReport;
			$data        = $orderReport->getChannelReport($request);

			return json(['code' => 0, 'msg' => '', 'count' => $data['total'], 'data' => $data['data']]);

		} else {

			$this->assign('seletime', $request['date']);

			$this->assign('bus_channel', db('bus_channel')->field('channel_id,company_name')->select());
			return $this->fetch();
		}
	}
	/**
	 * 月报表
	 * @Author   lf
	 * @DateTime 2019-01-04T14:44:03+0800
	 * @param    string                   $seletime [description]
	 * @return   [type]                             [description]
	 */
	public function indexYue() {
		$request = request()->only([
			'date'       => date("Y-m"),
			'channel_id' => '',
		]);

		$request['channel_id'] = session('relate_id');

		if (request()->isPost()) {
			$orderReport = new ChannelReport;
			$data        = $orderReport->getChannelMonthReport($request);

			return json(['code' => 0, 'msg' => '', 'count' => $data['total'], 'data' => $data['data']]);
		}

		$this->assign('request', $request);
		$this->assign('bus_channel', db('bus_channel')->field('channel_id,company_name')->select());
		return $this->fetch();

	}

	public function indexYueDao() {
		// set_time_limit(0); //超时不限制
		// //允许接收的请求变量
		// $getdata = request()->only(['seletime' => date("Y-m"), 'cid' => ''], 'GET');
		// if (session('user_type') == 'channel') {
		// 	$getdata['cid'] = session('relate_id');
		// }
		// if (empty($getdata['cid'])) {
		// 	exit('请选择渠道');
		// }
		// //文件名
		// $fileName = $getdata['cid'] . '_' . $getdata['seletime'] . '_' . date('YmdHis', time()) . rand(100, 999);
		// header('Content-Type: application/vnd.ms-execl');
		// header('Content-Disposition: attachment;filename="' . $fileName . '.csv');

		// if (session('user_type') == 'channel') {
		// 	$getdata['cid'] = session('relate_id');
		// }

		// $orderReport = new BusOrderReport;
		// $data        = $orderReport->where("date_format(create_time,'%Y-%m')='{$getdata['seletime']}'")->where('channel_id', $getdata['cid'])->select();

		// trace('用户' . session('username') . '导出报表条件：' . json_encode($getdata, JSON_UNESCAPED_UNICODE), 'info');

		// $titleData                   = ['日期', '总数', '成功', '失败', '未知', '金额']; //表头
		// $pullData['sms_all']         = 0;
		// $pullData['sms_success']     = 0;
		// $pullData['sms_faild']       = 0;
		// $pullData['sms_sending']     = 0;
		// $pullData['amt_sms_success'] = 0;

		// //生成临时文件
		// $fp = fopen('php://output', 'w');
		// fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF)); //转码
		// //输出表头
		// fputcsv($fp, $titleData);

		// foreach ($data as $key => $value) {
		// 	# code...
		// 	//输出
		// 	// $pullData['sms_all']         = bcadd($pullData['sms_all'],$value[1]['sms_all']);
		// 	// $pullData['sms_success']     = bcadd($pullData['sms_success'],$value[1]['sms_success']);
		// 	// $pullData['sms_faild']       = bcadd($pullData['sms_faild'],$value[1]['sms_faild']);
		// 	// $pullData['sms_sending']     = bcadd($pullData['sms_sending'],$value[1]['sms_sending']);
		// 	// $pullData['amt_sms_success'] = bcadd($pullData['amt_sms_success'],$value[1]['amt_sms_success'],3);

		// 	// fputcsv($fp, ["\t".$value[0],$value[1]['sms_all'],$value[1]['sms_success'],$value[1]['sms_faild'],$value[1]['sms_sending'],$value[1]['amt_sms_success']]);
		// 	$pullData['sms_all']         = bcadd($pullData['sms_all'], $value['sms_all']);
		// 	$pullData['sms_success']     = bcadd($pullData['sms_success'], $value['sms_success']);
		// 	$pullData['sms_faild']       = bcadd($pullData['sms_faild'], $value['sms_faild']);
		// 	$pullData['sms_sending']     = bcadd($pullData['sms_sending'], $value['sms_sending']);
		// 	$pullData['amt_sms_success'] = bcadd($pullData['amt_sms_success'], $value['amt_sms_success'], 5);

		// 	fputcsv($fp, ["\t" . $value['create_time'], $value['sms_all'], $value['sms_success'], $value['sms_faild'], $value['sms_sending'], $value['amt_sms_success']]);
		// }

		// fputcsv($fp, ['汇总', $pullData['sms_all'], $pullData['sms_success'], $pullData['sms_faild'], $pullData['sms_sending'], $pullData['amt_sms_success']]);

		// //输出缓存
		// @ob_flush();
		// flush();
		// ob_end_clean();
		// exit;
	}

	/**
	 * 导出日报表
	 * @Author    lf
	 * @DateTime  2020-04-20T15:09:21+0800
	 * @copyright [copyright]
	 * @license   [license]
	 * @version   [version]
	 * @return    [type]                   [description]
	 */
	public function export() {
		// $seletime = trim(input('get.seletime'));
		// $cha      = trim(input('get.cha'));
		// $cid      = trim(input('get.cid'));

		// if (empty($seletime)) {

		// 	return '需要选择时间';
		// }

		// set_time_limit(3 * 60);

		// $data = [];

		// if (session('user_type') == 'admin' || session('user_type') == 'clerk') {

		// 	$orderReport = new BusOrderReport;

		// 	if ($cha) {
		// 		$data = $orderReport->findReport($seletime, $cha);
		// 	} else {
		// 		$orderReport_data = cache('orderReport_data' . $seletime);
		// 		if (!$orderReport_data) {
		// 			$data = $orderReport->findReport($seletime, $cha);

		// 			cache('orderReport_data' . $seletime, $data, 30);
		// 		} else {
		// 			$data = $orderReport_data;
		// 		}
		// 	}
		// }
		// if (!$data) {
		// 	return '没有数据导出';
		// }

		// $writer = new XLSXWriter();
		// //文件名
		// $filename = $cha . "渠道日报表" . $seletime . ".xlsx";
		// //每列的标题头
		// $title = array(
		// 	'名称',
		// 	'运营商',
		// 	'短信总量',
		// 	'成功短信',
		// 	'失败短信',
		// 	'未知短信',
		// 	'成功百分比',
		// 	'失败百分比',
		// 	'未知百分比',
		// 	'总金额',
		// );
		// //工作簿名称
		// $sheet1 = 'sheet1';

		// //对每列指定数据类型，对应单元格的数据类型
		// foreach ($title as $key => $item) {
		// 	$col_style[]  = 'string';
		// 	$col_widths[] = '23';
		// }
		// //设置列格式，suppress_row: 去掉会多出一行数据；widths: 指定每列宽度,'widths'=>[20,20,20,20,20,20,20,20]
		// $writer->writeSheetHeader($sheet1, $col_style, ['suppress_row' => true, 'widths' => $col_widths]);
		// //写入第二行的数据，顺便指定样式
		// // $writer->writeSheetRow($sheet1, ['导出'],
		// // ['height'=>32,'font-size'=>16,'font-style'=>'bold','halign'=>'center','valign'=>'center']);

		// /*设置标题头，指定样式*/
		// $styles1 = array('font' => '宋体', 'font-size'  => 14, 'font-style'                        => 'bold', 'fill' => '#eee',
		// 	'halign'                => 'center', 'border' => 'left,right,top,bottom', 'border-style' => 'thin');

		// $writer->writeSheetRow($sheet1, $title, $styles1);
		// // 最后是数据，foreach写入
		// $count = count($data);

		// $styles2 = ['height' => 16, 'border' => 'left,right,top,bottom', 'border-style' => 'thin'];
		// //导出的数据
		// for ($i = 0; $i < $count; $i++) {
		// 	$row = array(
		// 		$data[$i]['company_name'],
		// 		isset($data[$i]['operator']) ? $data[$i]['operator'] : '',
		// 		$data[$i]['sms_all'],
		// 		$data[$i]['sms_success'],
		// 		$data[$i]['sms_faild'],
		// 		$data[$i]['sms_sending'],
		// 		$data[$i]['sms_success100'],
		// 		$data[$i]['sms_faild100'],
		// 		$data[$i]['sms_sending100'],
		// 		$data[$i]['amt_sms_success'],
		// 	);
		// 	$writer->writeSheetRow($sheet1, $row, $styles2);
		// }

		// //设置 header，用于浏览器下载
		// header('Content-disposition: attachment; filename="' . XLSXWriter::sanitize_filename($filename) . '"');
		// header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		// header('Content-Transfer-Encoding: binary');
		// header('Cache-Control: must-revalidate');
		// header('Pragma: public');

		// //合并单元格，第一行的大标题需要合并单元格
		// // $writer->markMergedCell($sheet1, $start_row=0, $start_col=0, $end_row=0, $end_col=7);
		// //输出文档
		// $writer->writeToStdOut();
		// exit(0);
	}
}
